package com.huarui.saleOrder.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

//import org.apache.log4j.Logger;
import org.ewaf.framework.action.BaseAction;

import com.huarui.params.Params;
import com.huarui.purchaseOrder.model.PurOrSaleOrder;
import com.huarui.saleOrder.enums.SaleOrderFunctionEnum;
import com.huarui.saleOrder.service.SaleOrderService;

public class SaleOrderQueryAction extends BaseAction{

	public static final String MESS = "mess";
	
	public static final String STATUS = "status";
	public static final String LOADED_MESS_KEYS = "loadedMesesKeys";
	
	private static final long serialVersionUID = 3296737304619520124L;

//	private Logger logger = Logger.getLogger(SaleOrderQueryAction.class);
	
	private SaleOrderService saleOrderService;
	
	private Integer start;
	
	private Integer saleOrderId;
	
	private String status;
	
	private String conditions;
		
	private Map<String, Object> mesMap ;
	
	public void setSaleOrderService(SaleOrderService saleOrderService) {
		this.saleOrderService = saleOrderService;
	}

	public Map<String, Object> getMesMap() {
		return mesMap;
	}

	public void setMesMap(Map<String, Object> mesMap) {
		this.mesMap = mesMap;
	}

	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public String getConditions() {
		return conditions;
	}

	public void setConditions(String conditions) {
		this.conditions = conditions;
	}

	public Integer getSaleOrderId() {
		return saleOrderId;
	}

	public void setSaleOrderId(Integer saleOrderId) {
		this.saleOrderId = saleOrderId;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	/**
	 * 单据查询或还原
	 * @return
	 */
	private String toShow() {
		mesMap = this.saleOrderService.toShowSaleOrderById(saleOrderId);
		return MESS;
	}
	
	public String onlineToShow() {
		return toShow();
	}
	
	public String notOnlineToShow() {
		return toShow();
	}
	
	/**
	 * 重置
	 * @return
	 */
	public String reset() {
		mesMap = new HashMap<String, Object>();
		mesMap.put(STATUS, PurOrSaleOrder.STATUS_INITIAL);
		mesMap.put(LOADED_MESS_KEYS, SaleOrderFunctionEnum.LOAD_BUTTONS_TOOL_BAR.getKey());
		return MESS;
	}
	
//	public String onlineReset() {
//		return reset();
//	}
//	
//	public String notOnlineReset() {
//		return reset();
//	}
	
	/**
	 * load主表
	 * @return
	 */
	private String loadSaleOrder() {
		mesMap = saleOrderService.loadSaleOrderById(saleOrderId);
		return MESS;
	}
	
	public String loadOnlineSaleOrder() {
		return loadSaleOrder();
	}
	
	public String loadNotOnlineSaleOrder() {
		return loadSaleOrder();
	}
	
	private String loadDetails() {
		List<Map<String, Object>> list = this.saleOrderService.loadDetailsBySaleOrderId(saleOrderId);
		mesMap = new HashMap<String, Object>();
		mesMap.put("saleDetails", list);
		return MESS;
	}
	
	public String loadOnlineDetails() {
		return loadDetails();
	}
	
	public String loadNotOnlineDetails() {
		return loadDetails();
	}
	
	/**
	 * 浏览概要
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private String listSaleOrders() {
		Map<String, Object> paramsMap = null;
		if(conditions != null) {
			JSONObject jo = JSONObject.fromObject(conditions);
			paramsMap = (Map<String, Object>) JSONObject.toBean(jo, HashMap.class);
		} else {
			paramsMap = new HashMap<String, Object>();
		}

		paramsMap.put("start", start==null ? 0 : start);
		paramsMap.put("pageSize", Params.PAGE_SIZE);
		mesMap = this.saleOrderService.querySaleOrdersListByComplex(paramsMap);
		return MESS;
	}
	
	public String listOnlineSaleOrders() {
		return listSaleOrders();
	}
	
	public String listNotOnlineSaleOrders() {
		return listSaleOrders();
	}
	
	/**
	 * 浏览明细
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private String listSaleDetails() {
		Map<String, Object> paramsMap = null;
		if(conditions != null) {
			JSONObject jo = JSONObject.fromObject(conditions);
			paramsMap = (Map<String, Object>) JSONObject.toBean(jo, HashMap.class);
		} else {
			paramsMap = new HashMap<String, Object>();
		}
		
		paramsMap.put("start", start==null ? 0 : start);
		paramsMap.put("pageSize", Params.PAGE_SIZE);
		mesMap = this.saleOrderService.querySaleDetailsListByComplex(paramsMap);
		return MESS;
	}
	
	public String listOnlineDetails() {
		return listSaleDetails();
	}
	
	public String listNotOnlineDetails() {
		return listSaleDetails();
	}
	
	@SuppressWarnings("unchecked")
	public String getAvailableOrders() {
		Map<String, Object> paramsMap = null;
		if(conditions != null) {
			JSONObject jo = JSONObject.fromObject(conditions);
			paramsMap = (Map<String, Object>) JSONObject.toBean(jo, HashMap.class);
		} else {
			paramsMap = new HashMap<String, Object>();
		}
		
		paramsMap.put("start", start==null ? 0 : start);
		paramsMap.put("pageSize", Params.PAGE_SIZE);
		
		mesMap = this.saleOrderService.getAvailableOrdersMess(paramsMap);
		return MESS;
	}
	
}
